package wfinder;

import static org.jlambda.util.Lists.map;
import static org.jlambda.util.Lists.mapMNonMem;
import static org.jlambda.util.Lists.seq;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeSet;

import org.jlambda.functions.Fun1;
import org.jlambda.functions.Fun2;

public class Printem extends Fun1< Map<String, Integer>, Void> {

	@Override
	public Void apply(final Map<String, Integer> counts) {
		final TreeSet<String> orderedByCounts = new TreeSet<String>(
				new Comparator<String>(){
					public int compare(String o1, String o2) {
						// strings if the count is the same
						Integer i = counts.get(o1);
						Integer j = counts.get(o2);
						if (i.compareTo(j) == 0) return o2.compareTo(o1);
						return j.compareTo(i);
					}});

		// sort em
		seq( mapMNonMem(new Fun2<String, Integer, Void>(){
			@Override
			public Void apply(String p1, Integer p2) {
				orderedByCounts.add(p1);
				return null;
			}
			}, counts) );
		
		/* print em */
		seq( map(new Fun1<String, Void>(){
			@Override
			public Void apply(String p1) {
				System.out.println(counts.get(p1) + " " + p1);
				return null;
			}
		}, orderedByCounts) );

		return null;
	}
	
}

